<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/dtree/dtree.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/dtree/font/dtreefont.css" media="all">
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }

    .el-input__suffix {
        position: absolute;
        height: 100%;
        right: 5px;
        top: 0;
        text-align: center;
        color: #c0c4cc;
        transition: all .3s;
        pointer-events: none;
    }
    .layui-form-label{
        width: 85px;
        position: relative;
        margin-left: -5px;
    }

</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="component-form-element" id="form" method="post" enctype="multipart/form-data">
                <div class="layui-tab-content" style="height: auto">
                   项目基本信息
                    <hr>
                    <div class="layui-row">

                                <div class="layui-col-lg6">
                                    <div class="layui-form-item">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">项目名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="name" lay-verify="name"
                                                   autocomplete="off" placeholder="请输入项目名称"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-col-xs1">
                                        <div class="layui-form-mid layui-word-aux"
                                             style="color: red !important;padding: 10px !important;">*
                                        </div>
                                    </div>
                                </div>
                                </div>
                                <div class="layui-col-lg6">
                                    <div class="layui-form-item">
                                        <div class="layui-col-xs12">
                                            <label class="layui-form-label">项目备注</label>
                                            <div class="layui-input-block">
                                                <input type="text" name="xmbz" lay-verify="xmbz"
                                                       autocomplete="off" placeholder="请输入项目备注"
                                                       class="layui-input">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                    <div class="layui-col-xs12">
                                        <label class="layui-form-label">项目开工时间</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="starDateString" id="starDateString" lay-verify="starDate"
                                                   autocomplete="off" placeholder="请输入项目开工时间"
                                                   class="layui-input">
                                        </div>
                                    </div>
                            </div>
                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">项目完工时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="enDateString" id="enDateString" lay-verify="enDate"
                                               autocomplete="off" placeholder="请输入项目完工时间"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">设备投产时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="tcDateString" id="tcDateString" lay-verify="tcDate"
                                               autocomplete="off" placeholder="设备投产时间"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">验收时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="ysDateString" id="ysDateString" lay-verify="ysDate"
                                               autocomplete="off" placeholder="请选择验收时间"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                    <div class="layui-row">
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">质检时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="zjDateString" id="zjDateString" lay-verify="zjDate"
                                               autocomplete="off" placeholder="请输入项目质检时间"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">出水排放标准</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="cspfbz" lay-verify="cspfbz"
                                               autocomplete="off" placeholder="请输入出水排放标准"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="layui-col-lg6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">工艺流程处理</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="gylccl" lay-verify="gylccl"
                                               autocomplete="off" placeholder="请输入工艺流程处理"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>

                    <div class="layui-row">
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">排污口</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="pwk" lay-verify="pwk"
                                               autocomplete="off" placeholder="请输入排污口"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">敷设管道长度</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="fsgdcd" lay-verify="fsgdcd"
                                               autocomplete="off" placeholder="请输入敷设管道长度"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">公里</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="gl" lay-verify="gl"
                                               autocomplete="off" placeholder="请输入公里"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="layui-col-lg3">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">化粪池</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="hfc" lay-verify="hfc"
                                               autocomplete="off" placeholder="请输入化粪池"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                    <div class="layui-row">
                        <div class="layui-col-lg6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">尾水排向</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="wspx" lay-verify="wspx"
                                               autocomplete="off" placeholder="尾水排向"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>

                        </div>
                        <div class="layui-col-lg6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">设备运行情况</label>
                                    <div class="layui-input-block">
                                        <select type="text" name="sbyxqk" lay-verify="sbyxqk"
                                               autocomplete="off" placeholder="请输入设备运行情况"
                                               class="layui-input">
                                            <option value="">请选择设备运行情况</option>
                                            <option value="正常运行">正常运行</option>
                                            <option value="调试运行">调试运行</option>
                                            <option value="在建">在建<option>
                                            <option value="未运行">未运行</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="layui-row">
                        <div class="layui-col-lg6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">设备厂家</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="sbcj" lay-verify="sbcj"
                                               autocomplete="off" placeholder="请输入设备厂家"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-lg6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs12">
                                    <label class="layui-form-label">日处理量(吨)</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="rcll" lay-verify="rcll"
                                               autocomplete="off" placeholder="请输入日处理量"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <input type="hidden" name="zrcids" id="zrcids">
                    </div>
                    乡镇基本信息
                    <hr>
                    <div class="layui-row">
                        <div class="layui-col-lg3">
                                <label class="layui-form-label">请选择乡镇</label>
                                <div class="layui-input-block">
                                    <select id="xz-select" lay-filter="xz-select"></select>
                                </div>
                        </div>

                        <div class="layui-col-lg3">
                            <label class="layui-form-label">请选择村委会</label>
                            <div class="layui-input-block">
                                <select id="cwh-select" lay-filter="cwh-select"></select>
                            </div>
                        </div>


                        <div class="layui-col-lg3">
                            <label class="layui-form-label">请选择自然村</label>
                            <div class="layui-input-block">
                                <select id="zrc-select" lay-filter="zrc-select"></select>
                            </div>
                        </div>
                        <div class="layui-col-lg1">
                            <span></span>
                        </div>
                        <div class="layui-col-lg1">
                            <button type="button" id="btn-addxz" class="layui-btn layui-btn-primary layui-btn">
                                <i class="layui-icon">&#xe654;</i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-row">
                        <table class="layui-hide" id="xztable" lay-filter="xztable"></table>
                    </div>



                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-footer" style="left: 0;padding: 0px;">
                        <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                    </div>
                </div>

            </form>

        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script type="text/html" id="toolBar-xz">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['form', 'laydate', 'layer'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate
            , layer = layui.layer;

        var  zrcSelecArr = []
        var zrcId = -1
        var xzTableData=[]

        //自定义验证规则
        form.verify({
            name: function (value, item) {
                if (value === "") {
                    return '请输入项目名称';
                }
            },
            enDateString: function (value, item) {
                if (value === "") {
                    return '请选择项目结束时间';
                }
            },
            tcDateString: function (value, item) {
                if (value === "") {
                    return '请选择项目投产时间';
                }
            },
            ysDateString: function (value, item) {
                if (value === "") {
                    return '请选择项目验收时间';
                }
            },
            starDateString: function (value, item) {
                if (value === "") {
                    return '请选择项目开工时间';
                }
            },
            sbyxqk: function (value, item) {
                if (value === "") {
                    return '请选择设备完成情况';
                }
            }
        });

        laydate.render({
            elem: '#starDateString' //指定元素
        });
        laydate.render({
            elem: '#enDateString' //指定元素
        });
        laydate.render({
            elem: '#tcDateString' //指定元素
        });
        laydate.render({
            elem: '#zjDateString' //指定元素
        });
        laydate.render({
            elem: '#ysDateString' //指定元素
        });

        //小表格
        layui.use(['table','layer'], function(){
            var table = layui.table
            var layer = layui.layer

            //第一个实例
            table.render({
                elem: '#xztable'
                ,data: xzTableData //数据接口
                ,page: false //开启分页
                ,cols: [[ //表头
                    {field: 'zhen', title: '乡镇'}
                    ,{field: 'cunweihui', title: '村委会'}
                    ,{field: 'name', title: '自然村(小组)'}
                    ,{field: 'hushu', title: '户数', sort: true}
                    ,{title: '操作', toolbar: '#toolBar-xz', align: "center", width: 250, fixed: "right"}

                ]]
                ,id :'xzxzTable'
                ,data:xzTableData
            });

            //添加按钮监听
            $(document).on('click','#btn-addxz',function (){
                var index = zrcSelecArr.indexOf(zrcId)
                if(zrcId == -1){
                    layer.msg("请选择自然村")
                }else if(index>-1){
                   layer.msg("此村庄已选择")
                }
                else {
                 $.ajax({
                     type: "post",
                     url: "<%=request.getContextPath()%>/rest/base/getXzByZrc",
                     dataType: "json",
                     async: true,
                     data:{
                         'zrcId':zrcId,
                     },
                     success:function (msg){
                         console.log(msg)
                         msg.zrc.zrcId =zrcId
                         xzTableData.push(msg.zrc)
                         zrcSelecArr.push(zrcId)
                         console.log(xzTableData)
                         table.reload('xzxzTable',{data:xzTableData})
                     }
                 })
                }
            })

            //表格删除事件
            table.on('tool(xztable)',function (obj){
                console.log(obj.data)
               var data = obj.data
               var selectId = data.zrcId
               for (var i =0;i<xzTableData.length;i++){
                    var xz = xzTableData[i]
                   console.log(xz.zrcId)
                    if(xz.zrcId == selectId){
                        console.log("删除")
                      xzTableData.splice(i,1)
                        console.log("xzTableData")
                    }
                }

                for (var i=0;i<zrcSelecArr.length;i++ ){
                   var id = zrcSelecArr[i]
                   if(id == selectId){
                     zrcSelecArr.splice(i,1)
                    }
                }
                table.reload('xzxzTable',{data:xzTableData})
            })

        });

        <%--$.ajax({--%>
        <%--    type: "get",--%>
        <%--    url: "<%=request.getContextPath()%>/rest/system/getSelectUserList",--%>
        <%--    dataType: "json",--%>
        <%--    async: false,--%>
        <%--    success: function (msg) {--%>
        <%--        var select = document.getElementById("operationperson");--%>
        <%--        $.each(msg.data,function(i,n) {--%>
        <%--            select.options.add(new Option(n.name,n.tid));--%>
        <%--        });--%>
        <%--        form.render('select','operationperson');--%>
        <%--    }--%>
        <%--});--%>



        $.ajax({
            type: "get",
            url: "<%=request.getContextPath()%>/rest/base/getXzglSelect",
            dataType: "json",
            async: false,
            data:{
                xzzdId:-1
            },
            success: function (msg) {
                document.getElementById("xz-select").options.length = 0;
                var content = '<option value="">请选择乡镇</option>';
                var  xzSelect = $("#xz-select");
                for (var i = 0;i<msg.xzzdList.length;i++){
                    content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';
                }
                xzSelect.append(content)
                form.render()
            }

        })


        //监听乡镇select
        form.on('select(xz-select)',function (data){
            //给村委会赋值
            var cwhselect = $("#cwh-select");
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/getXzglSelect",
                dataType: "json",
                async: false,
                data:{
                    'xzzdId':data.value,
                },
                success: function (msg) {
                    document.getElementById("cwh-select").options.length = 0;
                    var content = '<option value="">请选择村委会</option>';
                    var  cwhSelect = $("#cwh-select");
                    for (var i = 0;i<msg.xzzdList.length;i++){
                        content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';;
                    }
                    cwhSelect.append(content)
                    form.render();
                }
            })

        })

        //监听村委会select
        form.on('select(cwh-select)',function (data){
            //给自然村赋值
            var cwhselect = $("#zrc-select");
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/getXzglSelect",
                dataType: "json",
                async: false,
                data:{
                    'xzzdId':data.value,
                },
                success: function (msg) {
                    var  content = ""
                    document.getElementById("zrc-select").options.length = 0;
                    if(msg.xzzdList.length>0){
                        content = '<option value="">请选择自然村</option>';
                    }else {
                        content = '<option value="">没有可选择的自然村</option>';
                    }
                    var  zrcSelect = $("#zrc-select");
                    for (var i = 0;i<msg.xzzdList.length;i++){
                        content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';;
                    }
                    zrcSelect.append(content)
                    form.render();
                }
            })

        })

        //监听自然村select
        form.on('select(zrc-select)',function (data){
            zrcId = data.value
        })



        //第一种方法添加用户
        form.on('submit(*)', function (data) {
            var zrcIds =""

            for (var i=0;i<zrcSelecArr.length;i++){
                if(i==0){
                    zrcIds +=zrcSelecArr[i]
                }else {
                    zrcIds = zrcIds+'-'+zrcSelecArr[i]
                }
            }

            var field = data.field//当前容器的全部表单字段，名值对形式：{name: value}
            console.log("表单字段")
            console.log(field)
            var name=field.name
            var flag = true;
            field.zrcids = zrcIds
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/getBaseExist",
                dataType: "json",
                async: false,
                data: "name=" + name + "&field=projectTable",
                success: function (msg) {
                    flag = msg;
                }
            })
            if(!flag){
                layer.msg('对不起，该项目名称已存在');
                return  false;
            }
            var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
            $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/addProjectTable",
                data: field,
                dataType: "json",
                async: false,
                success: function (obj) {
                    setTimeout(function () {
                        layer.close(index);
                        if (obj === true) {
                            layer.msg('添加成功');
                        } else {
                            layer.msg(obj.errorMsg);
                        }
                    }, 2000);
                    setTimeout(function () {
                        var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                        parent.layer.close(indexs);//关闭弹出层
//                        window.parent.location.reload();//刷新父页面
                    }, 4000);
                }
            })
            return false;
        });
    });
</script>
</body>
</html>